<!DOCTYPE html>
<html lang="en">
<head>
    <title>管理员管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="demoTable">
    姓名：
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<!-- 自定义的控件 -->
<div id="toolbar" style="display: none; ">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add" data-method="notice" data-type="auto">添加</button>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="resetPassword">重置密码</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<div id="modal" style="display:none">
    <form class="layui-form" id="user-add" action="" lay-filter="entity">
        <input type="text" name="id" id="id" style="display:none">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="username" required lay-verify="required" placeholder="姓名" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">手机号：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="phone" required lay-verify="required|phone" placeholder="手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="email" required lay-verify="required|email" placeholder="邮箱" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label password">密码：</label>
            <div class="layui-input-inline layui-col-md4 password">
                <input type="text" name="password" required lay-verify="required" placeholder="密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="radio" name="sex" value="男" title="男" checked="">
                <input type="radio" name="sex" value="女" title="女">
            </div>
            <label class="layui-form-label">部门：</label>
            <div class="layui-input-inline layui-col-md4">
                <select name="departmentId" id="departmentId" lay-search="">
                    <option value="" >请选择</option>
                </select>
                <!--<input type="text" name="departmentId" required lay-verify="required" placeholder="部门"
                autocomplete="off" class="layui-input">-->
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职位：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="grade" placeholder="职位" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">待遇：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="treatmentPrice" placeholder="待遇" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="address" placeholder="地址" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">工作地址：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="workAddress" placeholder="工作地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">就业时间：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" class="layui-input" name="workDateTime" id="test2">
            </div>
            <label class="layui-form-label">就业状况：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="radio" name="workStatus" value="1" title="工作" checked="">
                <input type="radio" name="workStatus" value="2" title="休息">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出生日期：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" class="layui-input" name="workDateTime" id="test1">
            </div>
            <label class="layui-form-label">政治面貌：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="politicalStatus" placeholder="政治面貌" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">可挂号数：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="allowNum" lay-verify="number" placeholder="可挂号数" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">已挂号数：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="nowNum" lay-verify="number" placeholder="已挂号数" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色：</label>
            <div class="layui-input-inline">
                <div id="test7" name="role" class="demo-tree"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline layui-col-md12">
                <label class="layui-form-label"></label>
            </div>
            <label class="layui-form-label"></label>
            <div class="layui-input-inline layui-col-md12">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提
                    交</button>
                <button style="display: none;" type="reset" class="layui-btnlayui-btn-primary swiper-right-form-reset">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'layer', 'form', 'jquery'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var tree = layui.tree;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        laydate.render({
            elem: '#test1' //指定元素
        });
        laydate.render({
            elem: '#test2' //指定元素
        });
        $.get('/api/role/getRole', function (res) {
//开启复选框
            tree.render({
                elem: '#test7'
                , id: 'demoId'
                , data: res.data
                , showCheckbox: true
            });
        });
        $.get('/api/department/getDepartment', function (data) {
            $.each(data.data, function (index, item) {
                $('#departmentId').append(new Option(item.name, item.id));// 下
            });
            layui.form.render("select");//重新渲染 固定写法
        });
        table.render({
            elem: '#test'
            , url: '/api/user/page'
            , page: true //开启分页
            , id: 'testReload'
            , cols: [[
                {field: "id", title: "ID"},
                {field: "username", title: "姓名"},
                {field: "phone", title: "手机号"},
                {field: "email", title: "邮箱"},
                {field: "sex", title: "性别"},
                {field: "department", title: "科室"},
                {field: "address", title: "地址"},
                /*`allow_num` int(10) DEFAULT '0' COMMENT '医生：可挂号数',
                `now_num` int(10) DEFAULT '0' COMMENT '医生：已挂号数',*/
                {field: "workAddress", title: "工作地址"},
                {field: "workDateTime", title: "就业时间"},
                {field: "workStatus", title: "状况", templet: function (d) {return d.status === '1' ? "工作" : "休息";}},
                {field: "birthday", title: "出生日期"},
                {field: "politicalStatus", title: "政治面貌"},
                {field: "grade", title: "职位"},
                {field: "treatmentPrice", title: "待遇工资"},
                {field: "createDatetime", title: "创建时间", templet: "<div>{{layui.util.toDateString(d.createTime,'yyyyMM-dd HH:mm:ss')}}</div>"},
                {title: '操作', width: 200, toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar',//表格头信息
            response: { // 设置响应码
                statusCode: 1 //规定成功的状态码，默认：0
            },
            parseData: function (rs) {//数据格式解析
                if (rs.status == 1) {
                    return {
                        "code": rs.status,
                        "msg": rs.message,
                        "count": rs.total,
                        "data": rs.data
                    }
                }
            }
        });
//监听事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    $(".password").css("display","block");
//重载实例
                    tree.reload('demoId', {});
                    open2("添加页面", null);
                    break;
            }
        });
//监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'resetPassword') {
                layer.confirm('真的重置'+data.username+'的密码吗？', function
                    (index) {
                    $.ajax({
                        type: "get",
                        url: "/api/user/resetPassword/" + data.id,
                        dataType: "json",
                        success: function (da) {
                            if (da.status == 1) {
                                layer.msg(da.message)
                            } else {
                                layer.msg(da.message)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            } else if (obj.event === 'del') {
                layer.confirm('真的删除该条信息吗？', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/api/user/" + data.id,
                        dataType: "json",
                        success: function (da) {
                            if (da.status == 1) {
                                layer.msg(da.message)
                                obj.del(); //删除对应行（tr）的DOM结构
                            } else {
                                layer.msg(da.message)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                tree.reload('demoId', {});
                $(".password").css("display","none");
                $.get('/api/userRole/getUserRole?uid=' + data.id, function (res)
                {
                    tree.setChecked('demoId', res.data); //勾选指定节点
                });
                open2("修改页面", obj.data);
            }
        });
//"模态框",
        function open2(title, entity) {
            form.val("entity", entity);
            layer.open({
                type: 1
                , title: title
                , content: $("#modal")
                , offset: '10px'
                , area: ['800px', '95%']
                , end: function () {
                    $('.swiper-right-form-reset').click()
                    table.reload('testReload');
                }
            });
        }
//监听提交
        form.on('submit(formDemo)', function (data) {
            let data1 = form.val("entity");
            let roleIds = new Array();
            let checkedData = tree.getChecked('demoId'); //获取选中节点的数据
            $.each(checkedData, function (i, obj) {
                roleIds[i] = obj.id;
            });
            data1.roleIds = roleIds;
            var type = data1.id ? "put" : "post";
            $.ajax({
                url: '/api/user',
                type: type,
                dataType: "json",
                contentType: 'application/json',
                data: JSON.stringify(data1),
                success: function (da) {
                    if (da.status == 1) {
// layer.alert(da.message, function () {
                        layer.msg(da.message,{time:1*1000},function() {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(da.message);
                    }
                }
            });
            return false;
        });
        var $ = layui.$, active = {
            reload: function () {
                var name = $('#name');
//执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "role": name.val()
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>